//
//  GQHAgreementController.m
//  Xanthium
//
//  Created by GuanQinghao on 10/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHAgreementController.h"
#import "GQHHeader.h"


@interface GQHAgreementController () <UIWebViewDelegate> {
    
#pragma mark Data
    NSString *contentString;
#pragma mark View
    
}

@end


@implementation GQHAgreementController

#pragma mark --Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchAgreementData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    if (_qh_agreementType == GQHAgreementTypeRegistration) {
        
        self.qh_titleLabel.text = @"云交智运平台注册协议";
    } else if (_qh_agreementType == GQHAgreementTypeAuthorization) {
        
        self.qh_titleLabel.text = @"云交智运平台认证协议";
    } else if (_qh_agreementType == GQHAgreementTypeTransportation) {
        
        self.qh_titleLabel.text = @"云交智运平台运输协议";
    } else {
        
        self.qh_titleLabel.text = @"云交智运平台协议";
    }
    
    self.qh_rightButton.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate

#pragma mark --TargetMethod
- (void)qh_clickedLeftButton:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --PrivateMethod
/// 获取平台注册协议
- (void)fetchAgreementData {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (_qh_agreementType == GQHAgreementTypeRegistration) {
        
        [parameters setObject:@"01" forKey:@"largerType"];
    } else if (_qh_agreementType == GQHAgreementTypeAuthorization) {
        
        [parameters setObject:@"02" forKey:@"largerType"];
    } else if (_qh_agreementType == GQHAgreementTypeTransportation) {
        
        [parameters setObject:@"03" forKey:@"largerType"];
    } else {
        
        [self showToastWithText:@"未知协议类型"];
        return;
    }
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buLargeTextEditor/1/list"];
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            DLog(@"%@---%@",message,data);
            
            NSDictionary *contentDictionary = [[data objectForKey:@"list"] firstObject];
            contentString = [contentDictionary objectForKey:@"content"];
            [self loadWebView];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View
- (void)loadWebView {
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    [webView loadHTMLString:contentString baseURL:nil];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.qh_statusBarHeight + self.view.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}

@end
